﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using System.Xml.Serialization;
using System.IO;
using System.Collections;
using System.Xml;



namespace _3D_FileExplorer
{

    // Shopping list class which will be serialized
    [XmlRoot("TexturesDefinition")]
    public class TexturesDefinition
    {
        // This attribute enables the ArrayList to be serialized:
        [System.Xml.Serialization.XmlArray("Entry")]
        // Explicitly tell the serializer to expect the Item class
        // so it can be properly written to XML from the collection:
        [System.Xml.Serialization.XmlArrayItem("entry", typeof(Entry))]
        public ArrayList texDef;

        public TexturesDefinition()
        {
            texDef = new ArrayList();
  
        }

        public int AddEntry(Entry entry)
        {
            return texDef.Add(entry);
        }

        public int GetX(string type)
        {
            foreach (Entry en in texDef )
            {
                if (en.type == type)
                {
                    return en.x;
                }
            }

            int x = 1;
            return x;

        }
        public int GetY(string type)
        {
            foreach (Entry en in texDef)
            {
                if (en.type == type)
                {
                    return en.y;
                }
            }

            int y = 1;
            return y;

        }


        public static void SerializeToXML(TexturesDefinition td)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(TexturesDefinition));
            TextWriter textWriter = new StreamWriter(Environment.CurrentDirectory + @"/XMLfiles/texturesDef.xml");
            serializer.Serialize(textWriter, td);
            textWriter.Close();

        }
        public static TexturesDefinition DeserializeFromXML()
        {
            XmlSerializer deserializer = new XmlSerializer(typeof(TexturesDefinition));
            TextReader textReader = new StreamReader(Environment.CurrentDirectory + @"/XMLfiles/texturesDef.xml");
            
            TexturesDefinition td;
            td = (TexturesDefinition)deserializer.Deserialize(textReader);
            textReader.Close();

            return td;
        }
    }



    // Items in the shopping list
    public class Entry
    {
        [XmlAttribute("type")]
        public string type;
        [XmlAttribute("X")]
        public int x;
        [XmlAttribute("Y")]
        public int y;
        public Entry()
        {
        }

        public Entry(string type, int x, int y)
        {
            this.type = type;
            this.x = x;
            this.y = y;
        }
    }
}
